<template>
  <view class="container">
    <view class="prod-list" v-if="myFavoriteList.length">

      <view class="cont-item">

        <uni-swipe-action>
          <uni-swipe-action-item v-for="(item, index) in myFavoriteList" :key="index" :right-options="options"
            @click="delFavorite($event,item.id)" >
            <view class="show-item" :data-goodsId="item.id" @tap="toProdPage(item.id)">
              <view class="more-prod-pic">
                <image :src="item.picture" class="more-pic" />
              </view>
              <view class="prod-text-right">
                <view class="prod-text more">
                  {{ item.title }}
                </view>
                <view class="cate-prod-info">
                  {{ item.degree }}
                </view>
                <view class="prod-price more">
                  <text class="symbol">
                    ￥
                  </text>
                  <text class="big-num">
                    {{ wxs.parsePrice(item.price)[0] }}
                  </text>
                  <text class="small-num">
                    .{{ wxs.parsePrice(item.price)[1] }}
                  </text>
                </view>
              </view>
            </view>
          </uni-swipe-action-item>
        </uni-swipe-action>
      </view>

    </view>

    <view class="no-favorite" v-else>
      <image src="/static/images/icon/noFavorite.png" class="no-favorite-pic" />
      <view class="no-favorite-text">
        暂无收藏
      </view>

    </view>
  </view>
</template>

<script setup>
  import http from '../../utils/http';
  const wxs = number()
  /**
   * 生命周期函数--监听页面显示
   */
  onShow(() => {
    getMyFavorite()
  })

  let current = 1
  let size = 10
  //获取当前用户的收藏
  const myFavoriteList = ref([])
  const options = ref([{
    text: '取消收藏',
    style: {
      backgroundColor: '#ff0000',
    }
  }, ])
  const getMyFavorite = () => {
    http.request({
        url: '/favorite/queryList',
        method: 'GET',
        data: {
          current: current,
          size: size,
        }
      })
      .then((res) => {
        console.log(res);
        if (res.code === 1) {
          console.log("收藏");
          console.log(res);
          myFavoriteList.value = res.data.records
          console.log(myFavoriteList.value);
          console.log(myFavoriteList.value.length);
        }
      })
  }
  //去商品详情
  const toProdPage = (goodsId) => {
    uni.navigateTo({
      url: '/pages/prod/prod?goodsId=' + goodsId
    })
  }

  //取消收藏
  const delFavorite = (e, goodsId) => {
    console.log("删除id")
    console.log(goodsId);
    console.log(e);
    http.request({
      url:'/favorite/addOrCancel/'+goodsId,
      method: 'post',
    }).then((res) => {
      if (res.code == 1) {
        console.log("删除收藏成功");
        uni.showToast({
          title: '取消收藏成功',
          icon: "success"
        })
        getMyFavorite()
      }
    })
  }
</script>

<style scoped lang="scss">
  @import "./favorite.scss";
</style>
